package biz.obake.team.touchprotector.g;

import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;

public class d {
    /* access modifiers changed from: private */

    /* renamed from: a  reason: collision with root package name */
    public Handler f1710a = new Handler();

    /* renamed from: b  reason: collision with root package name */
    private SensorEventListener f1711b;

    /* renamed from: c  reason: collision with root package name */
    private int f1712c;
    /* access modifiers changed from: private */

    /* renamed from: d  reason: collision with root package name */
    public String f1713d;
    private SensorManager e;
    private Sensor f;
    /* access modifiers changed from: private */
    public int g;
    private boolean h = false;

    class a implements Runnable {
        a() {
        }

        public void run() {
            biz.obake.team.touchprotector.log.a b2;
            String format;
            d.b(d.this);
            if (d.this.i()) {
                b2 = biz.obake.team.touchprotector.log.a.b();
                format = String.format("%s: On after %d tries.", new Object[]{d.this.f1713d, Integer.valueOf(d.this.g)});
            } else if (d.this.g == 10) {
                b2 = biz.obake.team.touchprotector.log.a.b();
                format = String.format("%s: Failed after %d tries.", new Object[]{d.this.f1713d, Integer.valueOf(d.this.g)});
            } else {
                d.this.f1710a.postDelayed(this, 100);
                return;
            }
            b2.d(format);
        }
    }

    public d(SensorEventListener sensorEventListener, int i, int i2, String str) {
        this.f1711b = sensorEventListener;
        this.f1712c = i2;
        this.f1713d = str;
        this.h = false;
        SensorManager sensorManager = (SensorManager) biz.obake.team.android.a.a().getSystemService("sensor");
        this.e = sensorManager;
        this.f = sensorManager.getDefaultSensor(i);
    }

    static /* synthetic */ int b(d dVar) {
        int i = dVar.g;
        dVar.g = i + 1;
        return i;
    }

    /* access modifiers changed from: private */
    public boolean i() {
        Sensor sensor;
        SensorManager sensorManager = this.e;
        if (sensorManager == null || (sensor = this.f) == null) {
            return false;
        }
        return sensorManager.registerListener(this.f1711b, sensor, this.f1712c);
    }

    public Sensor f() {
        return this.f;
    }

    public boolean g() {
        return this.h;
    }

    public void h() {
        if (!this.h) {
            this.h = true;
            this.f1710a.removeCallbacksAndMessages((Object) null);
            this.g = 1;
            if (i()) {
                biz.obake.team.touchprotector.log.a.b().d(String.format("%s: On", new Object[]{this.f1713d}));
                return;
            }
            biz.obake.team.touchprotector.log.a.b().d(String.format("%s: Retrying...", new Object[]{this.f1713d}));
            this.f1710a.postDelayed(new a(), 100);
        }
    }

    public void j() {
        Sensor sensor;
        if (this.h) {
            this.h = false;
            this.f1710a.removeCallbacksAndMessages((Object) null);
            SensorManager sensorManager = this.e;
            if (sensorManager != null && (sensor = this.f) != null) {
                if (sensor != null) {
                    sensorManager.unregisterListener(this.f1711b, sensor);
                }
                biz.obake.team.touchprotector.log.a.b().d(String.format("%s: Off", new Object[]{this.f1713d}));
            }
        }
    }
}
